package com.jinshan.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinshan.edu.entity.EduChapter;
import com.jinshan.edu.entity.EduSection;
import com.jinshan.edu.mapper.EduChapterMapper;
import com.jinshan.edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinshan.edu.service.EduSectionService;
import com.jinshan.exception.EduException;
import com.jinshan.response.ChapterVO;
import com.jinshan.response.SectionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Jinshan
 * @since 2020-06-27
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduSectionService sectionService;

    @Override
    public void saveChapter(EduChapter chapter) {
        baseMapper.insert(chapter);
    }

    @Override
    public void updateChapter(EduChapter chapter) {
        baseMapper.updateById(chapter);
    }

    @Override
    public boolean deleteChapterById(String id) {
        QueryWrapper<EduSection> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        int count = sectionService.count(wrapper);
        if (count > 0) {
            throw new EduException(20001, "该章节存在小节, 不能直接被删除");
        }
        int rows = baseMapper.deleteById(id);
        return rows > 0;
    }

    @Override
    public List<ChapterVO> getAllChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> chapters = baseMapper.selectList(chapterQueryWrapper);

        QueryWrapper<EduSection> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("course_id", courseId);
        List<EduSection> sections = sectionService.list(sectionQueryWrapper);
        ArrayList<ChapterVO> chapterVOS = new ArrayList<>();

        for (EduChapter chapter : chapters) {
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(chapter,chapterVO);
            for (EduSection section : sections) {
                if (chapter.getId().equals(section.getChapterId())) {
                    SectionVO sectionVO = new SectionVO();
                    BeanUtils.copyProperties(section, sectionVO);
                    chapterVO.getSectionVOs().add(sectionVO);
                }

            }
            chapterVOS.add(chapterVO);
        }
        return chapterVOS;
    }

    @Override
    public EduChapter getChapterById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        baseMapper.delete(queryWrapper);
    }

}
